﻿namespace SomethingSpacial.AppServices.Web
{
    using System.Security.Authentication;
    using System.ServiceModel.DomainServices.Server;
    using System.Threading;
    using System.Collections.Generic;
    using System.Web.Security;
    using System.Security.Principal;
    using System.Linq;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server.ApplicationServices;

    /// <summary>
    ///    RIA Services DomainService responsible for authenticating users when
    ///    they try to log on to the application.
    ///    
    ///    Most of the functionality is already provided by the base class
    ///    AuthenticationBase
    /// </summary>
    [EnableClientAccess]
    public class AuthenticationService : AuthenticationBase<User> 
    {
        [RequiresRole("Admin")]
        public IEnumerable<User> GetAllUsers()
        {
            return Membership.GetAllUsers().Cast<MembershipUser>().Select(mu => this.GetUserForMembershipUser(mu));
        }

        private User GetUserForMembershipUser(MembershipUser membershipUser)
        {
            return this.GetAuthenticatedUser(
                new GenericPrincipal(new GenericIdentity(membershipUser.UserName), new string[0]));
        } 
    }
}
